package services

import (
	"fmt"
	"lottery/dao"
	"lottery/datasource"
	"lottery/models"
	"strconv"
	"time"
)

type UserdayService interface {
	//GetAll(page, size int) []models.LtUserday
	//CountAll() int64
	//Search(uid, day int) []models.LtUserday
	//Count(uid, day int) int
	//Get(id int) *models.LtUserday
	////Delete(id int) error
	Update(user *models.LtUserday, columns []string) error
	Create(user *models.LtUserday) error
	GetUserToday(uid int) *models.LtUserday
}

type userdayService struct {
	dao *dao.UserDayDao
}

func NewUserdayService() UserdayService {
	return &userdayService{
		dao: dao.NewUserDayDao(datasource.InstanceDB()),
	}
}

func (s *userdayService) GetUserToday(uid int) *models.LtUserday {
	// 拿到当前时间 转换为 int
	y, m, d := time.Now().Date()
	strDay := fmt.Sprintf("%d%02d%02d", y, m, d)
	day, _ := strconv.Atoi(strDay)
	list := s.dao.Search(uid, day)
	if len(list) > 0 {
		return &list[0] // 查询出来的话，拿到第一条数据
	} else {
		return nil
	}
}

func (s *userdayService) Update(data *models.LtUserday, columns []string) error {
	return s.dao.Update(data, columns)
}

func (s *userdayService) Create(data *models.LtUserday) error {
	return s.dao.Create(data)
}
